package com.archstoday.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.ScreenEntity;
import com.archstoday.utils.CodeUtils;
import com.archstoday.utils.ToolUtils;
import com.archstoday.utils.WechatUtils;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;

/**
 * 邀请好友页面
 */
public class InvitationActivity extends AsScrollBaseActivity {

    private TextView mTitleTv;
    private ImageView mCodeIcon;
    private ScreenEntity mScreenEntity;

    private String mShareUrl;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_invitation);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mCodeIcon = (ImageView) findViewById(R.id.invitation_code_icon);
    }

    @Override
    protected void initData(Bundle bundle) {
        mTitleTv.setText(R.string.user_invitation);
        if (StringUtil.isEmpty(MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""))){
            showToast(R.string.user_userid_null);
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            appFinish();
        } else {
            getInvitation();
        }
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
        setOnClickListener(R.id.share_wx_btn);
        setOnClickListener(R.id.share_group_btn);
        setOnClickListener(R.id.share_link_btn);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
            case R.id.share_wx_btn:
                if (!StringUtil.isEmpty(mShareUrl)) {
                    WechatUtils.share(mContext, getString(R.string.app_name),
                            getString(R.string.user_invitation), mShareUrl,
                            false);
                }
                break;
            case R.id.share_group_btn:
                if (!StringUtil.isEmpty(mShareUrl)) {
                    WechatUtils.share(mContext, getString(R.string.app_name),
                            getString(R.string.user_invitation), mShareUrl,
                            true);
                }
                break;
            case R.id.share_link_btn:
                if (!StringUtil.isEmpty(mShareUrl)) {
                    ToolUtils.copyUrl(mContext, mShareUrl);
                }
                break;
        }
    }

    private void getInvitation() {
        initParameter();
        mParams.put("selectType", "recommendUrl");

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_SELECT_BASE),
                AsRes.GET_SELECT_BASE, mParams, ScreenEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        if (client.getUrlIdentifier() == AsRes.GET_SELECT_BASE) {
            mScreenEntity = (ScreenEntity) parse;
            if (ToolUtils.isSuccess(mScreenEntity.getStatus())) {
                if (mScreenEntity.getData() != null) {
                    mShareUrl = mScreenEntity.getData().get(0).getLabel();
                    mShareUrl = mShareUrl.replace("xxx", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
                    Bitmap bitmap = CodeUtils.createQRImage(mShareUrl,
                            (int) mContext.getResources().getDimension(R.dimen.dp250),
                            (int) mContext.getResources().getDimension(R.dimen.dp250));
                    mCodeIcon.setImageBitmap(bitmap);
                }
            } else {
                showToast(mScreenEntity.getMsg());
            }
        }
    }
}